/////////////////////////////////////////////////////////////////////////////
// Name:        wordspanel.cpp
// Purpose:     
// Author:      Brant Young
// Modified by: 
// Created:     Mon May 14 21:46:51 2007
// RCS-ID:      
// Copyright:   Copyright (c) daodict.com 2007
// Licence:     GPL 2.0
/////////////////////////////////////////////////////////////////////////////
#include <wx/wx.h>
#include <wx/splitter.h>
#include <wx/imaglist.h>
#include <wx/srchctrl.h>

#include "../component/chmhtmlwnd.h" // must place to first, because i've change \
	redefined private/protected keywords
#include "wordswnd.h"
#include "../../core/thememgr.h"
#include "../../core/dictmgr.h"
#include "../../core/cfgmgr.h"
#include "../component/chmfindtoolbar.h"


/**
 * WordsWindow constructors
 */
WordsWindow::WordsWindow(wxWindow* p_parent) : wxPanel(p_parent, \
                                       ID_WORDS_WND, \
                                       wxDefaultPosition, wxDefaultSize, \
                                       wxNO_BORDER)
{
    // this->SetBackgroundColour(wxColour(247, 245, 204));
	_p_thememgr = &ThemeManager::get();
    _p_dictmgr = &DictManager::get();
    _p_cfgmgr = &ConfigManager::get();
    
    wxBoxSizer* p_main_sizer = new wxBoxSizer(wxVERTICAL);
    SetSizer(p_main_sizer);
    
    p_words_toolbar = new WordsToolBar(this);
    p_main_sizer->Add(p_words_toolbar, 0, wxEXPAND|wxBOTTOM, 1);

    p_spltwnd = new wxSplitterWindow(this, \
    		ID_WORDS_SPLTWND, \
            wxDefaultPosition, wxDefaultSize,
            wxNO_BORDER|wxTAB_TRAVERSAL);
    //p_spltwnd->SetBackgroundColour(wxColour(247, 245, 204));
    //p_spltwnd->SetSashSize(8);
    p_main_sizer->Add(p_spltwnd, 1, wxEXPAND);
    
    // ==== Contents HTML Window =====
    wxPanel* p_explain_panel = new wxPanel(p_spltwnd, wxID_ANY, \
    		wxDefaultPosition, wxDefaultSize, \
    		wxSUNKEN_BORDER);
    wxBoxSizer* p_explain_sizer = new wxBoxSizer(wxVERTICAL);
    p_explain_panel->SetSizer(p_explain_sizer);
    
    //p_explain_wnd = new ExplainWindow(p_explain_panel);
    //p_explain_sizer->Add(p_explain_wnd, 1, wxEXPAND, 0);
    p_explain_htmlwnd = new CHMHtmlWindow(p_explain_panel, ID_HTML_WND, wxDefaultPosition, wxDefaultSize);
    p_explain_sizer->Add(p_explain_htmlwnd, 1, wxEXPAND, 0);
    
    p_find_toolbar = new CHMFindToolBar(p_explain_panel, p_explain_htmlwnd);
    p_find_toolbar->Hide();
    p_explain_sizer->Add(p_find_toolbar, 0, wxEXPAND, 5);
    
    
    // ================   dict list   ===================
    wxPanel* p_dicts_panel = new wxPanel(p_spltwnd, wxID_ANY, \
    		wxDefaultPosition, wxDefaultSize, \
            wxNO_BORDER);
    wxBoxSizer* p_dicts_sizer = new wxBoxSizer(wxVERTICAL);
    p_dicts_panel->SetSizer(p_dicts_sizer);
    
    wxImageList* p_dictlist_imglist = new wxImageList(24, 24);
    wxBitmap& bmp = _p_thememgr->get_bitmap(_T("book-open-24.png"));
    p_dictlist_imglist->Add(bmp);
    bmp = _p_thememgr->get_bitmap(_T("book-fullopen-24.png"));
    p_dictlist_imglist->Add(bmp);
    
    p_dict_list_lbox = new wxListView(p_dicts_panel, ID_DICT_LIST_LBOX, wxDefaultPosition, wxDefaultSize, \
    					wxLC_REPORT|wxLC_ALIGN_LEFT|wxLC_SINGLE_SEL|wxLC_NO_HEADER|wxSUNKEN_BORDER); //wxSIMPLE_BORDER
    p_dict_list_lbox->SetImageList(p_dictlist_imglist, wxIMAGE_LIST_SMALL);
    p_dict_list_lbox->InsertColumn(0, _("Dict List"));
    // p_dict_list_lbox->SetColumnWidth(0, 130);
    
    p_dicts_sizer->Add(p_dict_list_lbox, 1, wxEXPAND, 0);

    // =============== Set sash pos =====================
    
    p_spltwnd->Initialize(p_explain_panel);
    p_spltwnd->Initialize(p_dicts_panel);
    p_spltwnd->SplitVertically(p_explain_panel,p_dicts_panel);
    p_spltwnd->SetSashGravity(0.0);
    p_spltwnd->SetMinimumPaneSize(2);

    
    InitEvents();
    
	/*wxAcceleratorEntry entries[2];
	entries[0].Set(wxACCEL_CTRL,  (int) 'F',     wxID_FIND);
	entries[1].Set(wxACCEL_CTRL,  (int) 'C',     wxID_FIND);
	wxAcceleratorTable accel(2, entries);
	this->SetAcceleratorTable(accel);*/
}


void WordsWindow::relayout()  {
    p_spltwnd->SetSashPosition(_p_cfgmgr->Read( \
    		_T("/Window/WordsWnd/sash-pos"), 570));
    _prev_sash_pos = _p_cfgmgr->Read( \
    		_T("/Window/WordsWnd/sash-pos"), 570);
    
    p_spltwnd->SetSashGravity(1.0); // a must
}

void WordsWindow::InitEvents() {
	this->Connect(WordsToolBar::ID_FIND, wxEVT_COMMAND_TOOL_CLICKED, 
			(wxObjectEventFunction)&WordsWindow::OnToolBarFind);
	this->Connect(wxID_FIND, wxEVT_COMMAND_TOOL_CLICKED, 
			(wxObjectEventFunction)&WordsWindow::OnFind);
	 
	this->Connect(ID_WORDS_SPLTWND, wxEVT_COMMAND_SPLITTER_DOUBLECLICKED, 
			(wxObjectEventFunction)&WordsWindow::OnSplitUnsplit);
	 
	this->Connect(ID_DICT_LIST_LBOX, wxEVT_COMMAND_LIST_ITEM_SELECTED, 
			(wxObjectEventFunction)&WordsWindow::OnDictSelected);
	this->Connect(ID_DICT_LIST_LBOX, wxEVT_COMMAND_LIST_ITEM_DESELECTED, 
			(wxObjectEventFunction)&WordsWindow::OnDictDeselected);
}


void WordsWindow::OnDictSelected(wxListEvent& event) {
	//p_search_ctrl->ChangeValue(event.GetText());
	_p_dictmgr->show_dict_explain(p_dict_list_lbox, event.GetIndex(), p_explain_htmlwnd);
	p_dict_list_lbox->SetItemImage(event.GetIndex(), 1);
	//wxMessageBox(_T("test"));
	//event.Skip();
}


void WordsWindow::OnDictDeselected(wxListEvent& event) {
	p_dict_list_lbox->SetItemImage(event.GetIndex(), 0);
}


void WordsWindow::OnToolBarFind(wxCommandEvent& event) {
		wxCommandEvent evt(event);
		evt.SetId(CHMFindToolBar::ID_CLOSE);
		wxPostEvent(p_find_toolbar, evt);
		// p_words_toolbar->EnableTool(WordsToolBar::ID_FIND, false);
		p_find_toolbar->p_find_srchctrl->SetSelection(-1,-1);
		p_find_toolbar->p_find_srchctrl->SetFocusFromKbd();
}


void WordsWindow::OnFind(wxCommandEvent& event) {
	if (false==p_find_toolbar->IsShown()) {
		wxCommandEvent evt(event);
		evt.SetId(CHMFindToolBar::ID_CLOSE);
		wxPostEvent(p_find_toolbar, evt);
	}
	
	p_find_toolbar->p_find_srchctrl->SetSelection(-1,-1);
	p_find_toolbar->p_find_srchctrl->SetFocusFromKbd();
}


void WordsWindow::OnSplitUnsplit(wxSplitterEvent& event) {
	
	int negative_sash_pos = p_spltwnd->GetClientSize().GetWidth() - \
		p_spltwnd->GetSashPosition() - p_spltwnd->GetSashSize();
	
	// wxMessageBox(wxString::Format(_("%d, %d, %d, %d"),negative_sash_pos, p_spltwnd->GetSashPosition(),p_spltwnd->GetClientSize().GetWidth(), p_spltwnd->GetSashSize()));
	
	if (negative_sash_pos>2) { // -> <-
		_prev_sash_pos = p_spltwnd->GetSashPosition();
		p_spltwnd->SetSashPosition(p_spltwnd->GetClientSize().GetWidth() - \
				p_spltwnd->GetSashSize()-2);
	} else { // <- ->
		p_spltwnd->SetSashPosition(_prev_sash_pos);
	}
}

/**
 * WordsWindow destructor
 */

WordsWindow::~WordsWindow() {
	_p_cfgmgr->Write(_T("/Window/WordsWnd/sash-pos"), p_spltwnd->GetSashPosition());
}


